package hxy.bytecode.designpattern;

/**
 * Description :
 * 利用静态内部类实现加锁，安全的单例模型
 *
 * @author eric
 * @date 11/5/19 2:59 PM
 */
public class SingletonApp {

    // 2. 私有的静态内部类，类加载器负责加锁
    private static class SingletonAppHolder {
        private static final SingletonApp singletonApp = new SingletonApp();
    }

    //1:私有化构造方法，只能通过getInstance方法获取实例
    private SingletonApp() {

    }

    // 3.自行对外提供实例
    public static SingletonApp getInstance() {
        return SingletonAppHolder.singletonApp;
    }

    public static void main(String[] args) {
        //测试
        SingletonApp singletonApp1 = SingletonApp.getInstance();
        SingletonApp singletonApp2 = SingletonApp.getInstance();

        if (singletonApp1.equals(singletonApp2)) {
            System.out.println("equal相等");
        }

        if (singletonApp1 == singletonApp2) {
            System.out.println("===相等");
        }

    }
}
